#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
记录模块数据模型
"""

from datetime import datetime
from bson import ObjectId

class RecordServiceModel:
    """记录服务模型"""
    
    def __init__(self, service_id=None, service_name=None, service_ip=None, service_port=None, is_active=True):
        self.id = str(ObjectId()) if service_id is None else service_id
        self.service_name = service_name
        self.service_ip = service_ip
        self.service_port = service_port
        self.is_active = is_active
        self.created_at = datetime.utcnow()
        self.updated_at = datetime.utcnow()
        
    def to_dict(self):
        """转换为字典"""
        return {
            "_id": self.id,
            "service_name": self.service_name,
            "service_ip": self.service_ip,
            "service_port": self.service_port,
            "is_active": self.is_active,
            "created_at": self.created_at,
            "updated_at": self.updated_at
        }
    
    @classmethod
    def from_dict(cls, data):
        """从字典创建对象"""
        if not data:
            return None
        
        service = cls(
            service_id=data.get("_id"),
            service_name=data.get("service_name"),
            service_ip=data.get("service_ip"),
            service_port=data.get("service_port"),
            is_active=data.get("is_active", True)
        )
        service.created_at = data.get("created_at", datetime.utcnow())
        service.updated_at = data.get("updated_at", datetime.utcnow())
        return service


class RequestRecordModel:
    """请求记录模型"""
    
    def __init__(self, record_id=None, service_id=None, client_service_name=None, server_service_name=None,
                 request_method=None, request_url=None, request_headers=None, request_body=None,
                 response_status=None, response_headers=None, response_body=None):
        self.id = str(ObjectId()) if record_id is None else record_id
        self.service_id = service_id
        self.client_service_name = client_service_name
        self.server_service_name = server_service_name
        self.request_method = request_method
        self.request_url = request_url
        self.request_headers = request_headers or {}
        self.request_body = request_body
        self.response_status = response_status
        self.response_headers = response_headers or {}
        self.response_body = response_body
        self.request_timestamp = datetime.utcnow()
        self.response_timestamp = datetime.utcnow()
        self.duration_ms = 0
        self.created_at = datetime.utcnow()
        
    def to_dict(self):
        """转换为字典"""
        return {
            "_id": self.id,
            "service_id": self.service_id,
            "client_service_name": self.client_service_name,
            "server_service_name": self.server_service_name,
            "request_method": self.request_method,
            "request_url": self.request_url,
            "request_headers": self.request_headers,
            "request_body": self.request_body,
            "response_status": self.response_status,
            "response_headers": self.response_headers,
            "response_body": self.response_body,
            "request_timestamp": self.request_timestamp,
            "response_timestamp": self.response_timestamp,
            "duration_ms": self.duration_ms,
            "created_at": self.created_at
        }
    
    @classmethod
    def from_dict(cls, data):
        """从字典创建对象"""
        if not data:
            return None
        
        record = cls(
            record_id=data.get("_id"),
            service_id=data.get("service_id"),
            client_service_name=data.get("client_service_name"),
            server_service_name=data.get("server_service_name"),
            request_method=data.get("request_method"),
            request_url=data.get("request_url"),
            request_headers=data.get("request_headers", {}),
            request_body=data.get("request_body"),
            response_status=data.get("response_status"),
            response_headers=data.get("response_headers", {}),
            response_body=data.get("response_body")
        )
        record.request_timestamp = data.get("request_timestamp", datetime.utcnow())
        record.response_timestamp = data.get("response_timestamp", datetime.utcnow())
        record.duration_ms = data.get("duration_ms", 0)
        record.created_at = data.get("created_at", datetime.utcnow())
        return record
    
    def to_request_definition(self):
        """转换为请求定义格式"""
        return {
            "name": f"{self.request_method} {self.request_url}",
            "description": f"Recorded from {self.client_service_name or 'unknown'} to {self.server_service_name}",
            "request_method": self.request_method,
            "request_url": self.request_url,
            "request_headers": self.request_headers,
            "request_body": self.request_body
        }
